package com.tony.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC {

	private String DRIVER_NAME;

	private String url;
	private String username;
	private String password;

	public JDBC(String dRIVER_NAME, String url, String username, String password) {
		super();
		DRIVER_NAME = dRIVER_NAME;
		this.url = url;
		this.username = username;
		this.password = password;
		try {
			Class.forName(DRIVER_NAME);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}

	public Statement getStatement(Connection connection) throws SQLException {
		return connection.createStatement();
	}

	public void execSqlU(String sql) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = getConnection();
			stmt = getStatement(conn);
			stmt.executeUpdate(sql);
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

}
